﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WHC.Security.Entity;

namespace CKNetFrameWork
{
    public partial class Frm_Login : Form
    {
        private LSY.BLL.LSY_AgentTab m_bllAccounts_Users = new LSY.BLL.LSY_AgentTab();
        public Dictionary<string, FunctionInfo> FunctionDict = new Dictionary<string, FunctionInfo>();

        public Frm_Login()
        {
            InitializeComponent();

        }

        private void buttonLogin_Click(object sender, EventArgs e)
        {
            if (this.comboBoxOrderPerson.Text == "")
            {
                this.errorProvider1.SetError(this.comboBoxOrderPerson, "用户名称不能为空！");
                return;
            }
            else
            {
                this.errorProvider1.SetError(this.comboBoxOrderPerson, "");
            }

            //if (this.textBox_password.Text == "")
            //{
            //    this.errorProvider1.SetError(this.textBox_password, "密码不能为空！");
            //    return;
            //}
            //else
            //{
            //    this.errorProvider1.SetError(this.textBox_password, "");
            //}
            //string loginName = this.cmbzhanhao.Text.Trim();
         
            string strPass = this.textBox_password.Text;

            string identity = WHC.Security.BLL.BLLFactory<WHC.Security.BLL.User>.Instance.VerifyUser(this.comboBoxOrderPerson.Text, strPass, Guid.NewGuid().ToString());

            DataSet dsLogin = m_bllAccounts_Users.GetUserInfoByLoginInfo(this.comboBoxOrderPerson.Text, strPass);
            if (!string.IsNullOrEmpty(identity))
            {
                UserInfo info = WHC.Security.BLL.BLLFactory<WHC.Security.BLL.User>.Instance.GetUserByName(this.comboBoxOrderPerson.Text);
                Frm_Main.m_nLoginUserId = info.ID;
                #region 获取用户的功能列表

                //List<FunctionInfo> list = WHC.Security.BLL.BLLFactory<WHC.Security.BLL.Function>.Instance.GetFunctionsByUser(info.ID, "WareMis");
            
                //if (list != null && list.Count > 0)
                //{
                //    foreach (FunctionInfo functionInfo in list)
                //    {
                //        if (!Program.gc.FunctionDict.ContainsKey(functionInfo.Name))
                //        {
                //            Program.gc.FunctionDict.Add(functionInfo.Name, functionInfo);
                //        }
                //    }
                //}
                this.DialogResult = DialogResult.OK;
                #endregion


            }
            else
            {
                MessageBox.Show("用户名或密码错误,请重新输入！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.textBox_password.Text = "";

                return;
            }
            //DataSet dsLogin = m_bllAccounts_Users.GetUserInfoByLoginInfo(this.textBox_username.Text, this.textBox_password.Text);
            //if (dsLogin.Tables[0].Rows.Count > 0)
            //{
            //    Frm_Main.m_nLoginUserId = (int)dsLogin.Tables[0].Rows[0]["LSY_AgentTabid"];
            //    this.DialogResult = DialogResult.OK;
            //    //得到该登录用户的权限
            //}
            //else
            //{
        
            //    MessageBox.Show("用户名或密码错误,请重新输入！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //    this.textBox_password.Text = "";

            //    return;
            //}
        }

        private void buttonExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void Frm_Login_Load(object sender, EventArgs e)
        {
            //DataSet ds = m_bllAccounts_Users.GetList("");
            //if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            //{
            //    this.comboBoxOrderPerson.DataSource = ds.Tables[0];
            //    this.comboBoxOrderPerson.DisplayMember = "代理商登录用户名";
            //    this.comboBoxOrderPerson.ValueMember = "LSY_AgentTabID";
            //    this.comboBoxOrderPerson.AutoCompleteSource = AutoCompleteSource.ListItems;
            //    this.comboBoxOrderPerson.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            //    this.comboBoxOrderPerson.SelectedIndex = 0;
            //}
            List<UserInfo> userList = WHC.Security.BLL.BLLFactory<WHC.Security.BLL.User>.Instance.GetAll();
            this.comboBoxOrderPerson.Items.Clear();
            foreach (UserInfo info in userList)
            {
                this.comboBoxOrderPerson.Items.Add(info.Name);
            }

        }

        private void textBox_username_TextChanged(object sender, EventArgs e)
        {
            //this.textBox_username.Focus();
        }

        private void Frm_Login_KeyDown(object sender, KeyEventArgs e)
        {

        }

        private void textBox_username_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                buttonLogin_Click(this, null);
        }

        private void comboBoxOrderPerson_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                buttonLogin_Click(this, null);
        }

        private void textBox_password_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                buttonLogin_Click(this, null);
        }
    }
}
